﻿using System;

using Fuse8.DomainFramework.Common;

using Fuse8.ContentManagementFramework.SystemFacade.Extensions;

namespace Fuse8.ContentManagementFramework.SystemFacade.Utilities
{
    public static class EntitiesConverter
    {
        public static object ConvertToType(string valueType, string val)
        {
            Guard.ArgumentNotEmpty(valueType, "valueType");

            object result = null;

            if (!string.IsNullOrWhiteSpace(val))
            {
                var concreteType = Type.GetType(valueType);
                if (concreteType != null)
                {
                    if (!concreteType.IsEnum)
                    {
                        result = val.DigUpFromString(concreteType);
                    }
                    else
                    {
                        result = Enum.Parse(concreteType, val, true);
                    }
                }
            }

            return result;
        }
    }
}
